
package com.market.web.action;

import java.util.List;

import com.market.bean.Product;
import com.market.bean.Supplier;
import com.market.web.form.ProCriForm;
import com.market.web.form.ProductForm;
import com.market.web.utils.ListUtils;


public class ProductAction extends BaseAction2
{

    public ProductAction()
    {
    }

    protected void setOtherProduct(Product pro)
    {
        List spp = getProductService().findSPPro(pro.getPrice(), pro.getCategory());
        getRequest().setAttribute("spPro", ListUtils.convert(spp, com.market.bean.Product.class,
                com.market.web.form.ProductForm.class));
        List stp = getProductService().findSTPro(pro.getType(), pro.getCategory());
        getRequest().setAttribute("stPro", ListUtils.convert(stp, com.market.bean.Product.class,
                com.market.web.form.ProductForm.class));
        List proCri = getProCriService().findByProduct(pro.getId(), 0, 4);
        int criCount = getProCriService().getCountByProduct(pro.getId());
        getRequest().setAttribute("criCount", String.valueOf(criCount));
        List proCrif = ListUtils.convert(proCri, com.market.bean.Productcritique.class,
                com.market.web.form.ProCriForm.class);
        for (int i = 0; i < proCrif.size(); i++)
        {
            ProCriForm pcf = (ProCriForm)proCrif.get(i);
            String critique = pcf.getCritique();
            int length = critique.length();
            String str = "";
            if (length > 100)
                str = "...";
            pcf.setCritique(critique.substring(0, length <= 100 ? length : 100) + str);
        }

        getRequest().setAttribute("proCri", proCrif);
    }

    protected void setSSProduct( ProductForm prof)
    {
        long supId = prof.getSupplierid();
        Supplier supplier = getSupplierService().findById(supId);
        prof.setSuppliername(supplier.getName());
        prof.setSupplierid(supplier.getId());
        List ssp = getProductService().findSSPro(supplier);
        getRequest().setAttribute("ssPro", ListUtils.convert(ssp, com.market.bean.Product.class,
                com.market.web.form.ProductForm.class));
    }

    protected void setOtherInfo(String category)
    {
        getRequest().setAttribute("news", ListUtils.convert(getMessageService().findNewsByCategory(category, 6),
                com.market.bean.Message.class, com.market.web.form.MessageForm.class));
        getRequest().setAttribute("knowledge", ListUtils.convert(getMessageService().findKnowByCategory(category, 6),
                com.market.bean.Message.class, com.market.web.form.MessageForm.class));
        getRequest().setAttribute("newPro", ListUtils.convert(getProductService().getNewList(category, 6),
                com.market.bean.Product.class, com.market.web.form.ProductForm.class));
        getRequest().setAttribute("hotPro", ListUtils.convert(getProductService().getHotList(category, 6),
                com.market.bean.Product.class, com.market.web.form.ProductForm.class));
    }

    protected long getProductId()
    {
        long id = 0L;
        String sId = getRequest().getParameter("productid");
        try
        {
            id = Long.parseLong(sId);
        }
        catch (Exception exception)
        {
            logger.error("",exception);
        }
        return id;
    }

/*    protected void setPages(int curP, int count,
            int perPage)
    {
        Pages pages = new Pages();
        pages.setPerPage(perPage);
        pages.setRowCount(count);
        pages.setCurPage(curP);
        StringBuffer path = new StringBuffer();
        path.append(getRequest().getServletPath()).append(".do?").append("category=").append(sf.getCategory()).append("&type=")
                .append(sf.getType()).append("&name=").append(sf.getName()).append("&parameter=").append(
                        sf.getParameter()).append("&orderBy=").append(sf.getOrderBy());
        pages.setPath(path.toString());
        getRequest().setAttribute("pages", pages);
    }*/
}
